package com.library.library.controller;

import com.library.library.common.ServerResponse;
import com.library.library.entity.Book;
import com.library.library.model.pager.PagerService;
import com.library.library.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Auther: 杜習田
 * @Date: 2020/2/27 19:37
 * @Description:
 */
@RestController
@RequestMapping("/book")
@Slf4j
public class BookController {

    @Autowired
    private BookService bookService;
    @Autowired
    private PagerService pagerService;

    @PostMapping("findAll")
    private ServerResponse findAll(){
        List<Book> books = bookService.findAll();
        return ServerResponse.createBySuccess(books);
    }
    @PostMapping("addBook")
    private ServerResponse addBook(Book book){
        book.setStatus("在库");
        bookService.save(book);
        log.info(String.format("%s 图书添加成功！",book.getName()));
        return ServerResponse.createBySuccessMessage("添加成功！");
    }
    @PostMapping("findPage")
    private ServerResponse findPage(int pageNum,int pageSize){
        Pageable pageable = pagerService.defPager(pageNum,pageSize);
        Page<Book> books = bookService.findAll(pageable);
        return ServerResponse.createBySuccess(books);
    }
}
